package com.jintoufs.util.convert;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/7/3.
 */
public class StringDateConverter implements Converter<String, Date> {

    private static final List<String> formarts = new ArrayList<String>(4);

    static {
        formarts.add("yyyyMM");
        formarts.add("yyyy-MM");
        formarts.add("yyyy年MM月");
        formarts.add("yyyy-MM-dd");
        formarts.add("yyyy年MM月dd日");
        formarts.add("yyyy-MM-dd hh:mm");
        formarts.add("yyyy-MM-dd hh:mm:ss");
        formarts.add("yyyy.MM.");
        formarts.add("yyyy.MM");
        formarts.add("yyyy.MM.dd");
    }

    public Date convert(String source) {
        String value = source.trim();
        if ("".equals(value)) {
            return null;
        }
        if (source.matches("^2\\d{5}$")) {
            return parseDate(source, formarts.get(0));
        } else if (source.matches("^\\d{4}-\\d{1,2}$")) {
            return parseDate(source, formarts.get(1));
        } else if (source.matches("^\\d{4}年\\d{1,2}月$")) {
            return parseDate(source, formarts.get(2));
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
            return parseDate(source, formarts.get(3));
        } else if (source.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日$")) {
            return parseDate(source, formarts.get(4));
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
            return parseDate(source, formarts.get(5));
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
            return parseDate(source, formarts.get(6));
        } else if (source.matches("^\\d{4}\\.\\d{1,2}\\.$")) {
            return parseDate(source, formarts.get(7));
        } else if (source.matches("^\\d{4}\\.\\d{1,2}$")) {
            return parseDate(source, formarts.get(8));
        } else if (source.matches("^\\d{4}\\.\\d{1,2}\\.\\d{1,2}$")) {
            return parseDate(source, formarts.get(9));
        } else if (source.matches("\\d{4,}\\.*\\d*$")) {
            try {
                Double db = Double.parseDouble(source);
                Date d = new Date();
                d.setTime(db.longValue());
                return d;
            } catch (Exception e) {
                throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
            }
        } else {
            throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
        }
    }

    /**
     * 功能描述：格式化日期
     *
     * @param dateStr String 字符型日期
     * @param format  String 格式
     * @return Date 日期
     */
    public Date parseDate(String dateStr, String format) {
        Date date = null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(format);
            date = (Date) dateFormat.parse(dateStr);
        } catch (Exception e) {
        }
        return date;
    }

    public static String numberFormat(String number) {
        String result = "";
        DecimalFormat myFormat = new DecimalFormat();
        myFormat.applyPattern("###.00");
        if (number != null && !"".equals(number)) {
            result = myFormat.format(Double.parseDouble(number));
        }
        return result;
    }

    public static int formatCurrency(String val) {
        String reg = "([\u4e00-\u9fa5]+)";
        Matcher matcher = Pattern.compile(reg).matcher(val);
        String chinese = "";
        while (matcher.find()) {
            chinese += matcher.group(0);
        }
        int number = Integer.parseInt(val.replace(chinese, ""));
        if (chinese.indexOf("角") >= 0) {
            number = number / 10;
        }
        return number;
    }
}
